# 1.导包
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from faker import Faker

fk = Faker('zh_CN')
# 2.打开浏览器（创建浏览器驱动对象）
path = r"C:\Program Files\Python311\chromedriver.exe"
ser = Service(executable_path=path)  # Chrome浏览器驱动服务对象
driver = webdriver.Chrome(service=ser)  # 打开Chrome浏览器
# 3.输入网址
driver.get("http://121.43.169.97:8081/2. ")
# 4.页面操作
driver.find_element(By.ID, value='keywords').send_keys('15532542150')
driver.find_element(By.ID, value='password').send_keys('1q2w3e')
driver.find_element(By.ID, value='login-btn').click()
# time.sleep(2)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, '立即开通'))).click()
# WebDriverWait(driver, 10).until(lambda x: x.find_element(By.LINK_TEXT, value='立即开通').click())
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, 'realname'))).send_keys(fk.name())
# WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "realname"))).send_keys("梁为我")
driver.find_element(By.NAME, 'card_id').send_keys(fk.ssn())
# driver.find_element(By.LINK_TEXT,value='立即开通').click()
# fr_ele=driver.find_element(By.ID,value='safeName')
# driver.switch_to.frame(fr_ele)
# time.sleep(2)
driver.find_element(By.CSS_SELECTOR, '[value="确认提交"]').click()
# WebDriverWait(driver,10).until(lambda x:x.find_element(By.CSS_SELECTOR,'[value="立即开通"]')).clike()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.btn.ng-scope'))).click()
time.sleep(1)
handles = driver.window_handles
driver.switch_to.window(handles[1])
result = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body"))).text
# 结果断言： body标签的文本
assert "UserRegister OK" in result
# driver.find_element(By.CSS_SELECTOR,'[value="立即开通"]').click()

# 5.等待2秒
time.sleep(2)
# 6.退出浏览器
driver.quit()
